package com.tianmu.demo.ad;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.tianmu.ad.SplashAd;
import com.tianmu.ad.bean.SplashAdInfo;
import com.tianmu.ad.error.TianmuError;
import com.tianmu.ad.listener.SplashAdListener;
import com.tianmu.utils.TianmuLogUtil;
import com.tianmu.demo.MainActivity;
import com.tianmudemo.R;
import com.tianmu.demo.constant.TianmuDemoConstant;

/**
 * @Description:
 * @Author: maipian
 * @CreateDate: 2021/9/8 8:06 PM
 */
public class SplashAdActivity extends AppCompatActivity {

    private SplashAd splashAd;
    private FrameLayout flContainer;
    private TextView tvSkip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        flContainer = findViewById(R.id.flContainer);
        tvSkip = findViewById(R.id.tvSkip);

        splashAd = new SplashAd(this, flContainer);
//        splashAd = new SplashAd(this, flContainer, tvSkip);
//        splashAd.setCountDownTime(5000);
        splashAd.setListener(new SplashAdListener() {
            @Override
            public void onAdTick(long millisUntilFinished) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onADTick millisUntilFinished:" + millisUntilFinished);
                tvSkip.setText(millisUntilFinished + "/跳过");
            }

            @Override
            public void onAdReceive(SplashAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onAdReceive");
            }

            @Override
            public void onAdExpose(SplashAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onAdExpose");
            }

            @Override
            public void onAdClick(SplashAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onAdClick");
            }

            @Override
            public void onAdSkip(SplashAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onADSkip");
            }

            @Override
            public void onAdClose(SplashAdInfo adInfo) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG,"onAdClose");
                toMain();
            }

            @Override
            public void onAdFailed(TianmuError tianmuError) {
                TianmuLogUtil.d(TianmuDemoConstant.TAG, "onAdFailed error" + tianmuError.getError());
                toMain();
            }
        });
        splashAd.loadAd(TianmuDemoConstant.SPLASH_ID);
    }

    private void toMain() {
        startActivity(new Intent(this, MainActivity.class));
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (splashAd != null) {
            splashAd.release();
        }
    }
}
